/**
 * All rights Reserved, Designed By 林溪
 * Copyright:    Copyright(C) 2016-2020
 * Company       溪云阁 .
 */

package com.boots.register.view.register.view;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.module.boots.api.message.ResponseMsg;
import com.module.boots.api.utils.MsgUtils;
import com.module.boots.exception.CommonRuntimeException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;

/**
 * Nacos控制器
 * @author：溪云阁
 * @date：2020年6月12日
 */
@Api(tags = { "web服务：查询注册中心上的信息" })
@RestController
@RequestMapping("view/minio")
public class NacosView {

    @NacosInjected
    private NamingService namingService;

    @NacosValue(value = "${project.name:null}", autoRefreshed = true)
    private String projectName;

    /**
     * 获取项目名称
     * @author 溪云阁
     * @return ResponseMsg<String>
     */
    @ApiOperation(value = "获取项目名称")
    @GetMapping(value = "/getProjectName")
    @SneakyThrows(CommonRuntimeException.class)
    public ResponseMsg<String> getProjectName() {
        return MsgUtils.buildSuccessMsg(projectName);
    }

    /**
     * 查询所有服务
     * @author 溪云阁
     * @param serviceName
     * @return ResponseMsg<List<Instance>>
     */
    @ApiOperation(value = "查询所有服务")
    @GetMapping(value = "/getAllServices")
    @SneakyThrows({ CommonRuntimeException.class, NacosException.class })
    public ResponseMsg<List<Instance>> getAllServices(@RequestParam("serviceName") String serviceName) {
        return MsgUtils.buildSuccessMsg(namingService.getAllInstances(serviceName));
    }

}
